/*
 * dio.c
 *
 * Created: 6/18/2013 12:23:16 AM
 *  Author: Tim
 */ 

#include <stdbool.h>
#include "dio.h"
#include "../allocations.h"


void dioTgl(uint8_t pinNumber)
{
	switch(pinNumber)
	{
		case 0:
		PORTC.OUTTGL = D0_msk;
		break;
		case 1:
		PORTD.OUTTGL = D1_msk;
		break;
		case 2:
		PORTD.OUTTGL = D2_msk;
		break;
		case 3:
		PORTD.OUTTGL = D3_msk;
		break;
		case 4:
		PORTD.OUTTGL = D4_msk;
		break;
		case 5:
		PORTD.OUTTGL = D5_msk;
		break;
		case 6:
		PORTD.OUTTGL = D6_msk;
		break;
		case 7:
		PORTR.OUTTGL = D7_msk;
		break;
		case 8:
		PORTR.OUTTGL = D8_msk;
		break;
		case 9:
		PORTE.OUTTGL = D9_msk;
		break;
		case 10:
		PORTE.OUTTGL = D10_msk;
		break;
		case 11:
		PORTE.OUTTGL = D11_msk;
		break;
		case 12:
		PORTE.OUTTGL = D12_msk;
		break;
		
	}
}

void dioSet(uint8_t pinNumber)
{
	switch(pinNumber)
	{
		case 0:
		PORTC.OUTSET = D0_msk;
		break;
		case 1:
		PORTD.OUTSET = D1_msk;
		break;
		case 2:
		PORTD.OUTSET = D2_msk;
		break;
		case 3:
		PORTD.OUTSET = D3_msk;
		break;
		case 4:
		PORTD.OUTSET = D4_msk;
		break;
		case 5:
		PORTD.OUTSET = D5_msk;
		break;
		case 6:
		PORTD.OUTSET = D6_msk;
		break;
		case 7:
		PORTR.OUTSET = D7_msk;
		break;
		case 8:
		PORTR.OUTSET = D8_msk;
		break;
		case 9:
		PORTE.OUTSET = D9_msk;
		break;
		case 10:
		PORTE.OUTSET = D10_msk;
		break;
		case 11:
		PORTE.OUTSET = D11_msk;
		break;
		case 12:
		PORTE.OUTSET = D12_msk;
		break;
		
	}
}

void dioClr(uint8_t pinNumber)
{
	switch(pinNumber)
	{
		case 0:
		PORTC.OUTCLR = D0_msk;
		break;
		case 1:
		PORTD.OUTCLR = D1_msk;
		break;
		case 2:
		PORTD.OUTCLR = D2_msk;
		break;
		case 3:
		PORTD.OUTCLR = D3_msk;
		break;
		case 4:
		PORTD.OUTCLR = D4_msk;
		break;
		case 5:
		PORTD.OUTCLR = D5_msk;
		break;
		case 6:
		PORTD.OUTCLR = D6_msk;
		break;
		case 7:
		PORTR.OUTCLR = D7_msk;
		break;
		case 8:
		PORTR.OUTCLR = D8_msk;
		break;
		case 9:
		PORTE.OUTCLR = D9_msk;
		break;
		case 10:
		PORTE.OUTCLR = D10_msk;
		break;
		case 11:
		PORTE.OUTCLR = D11_msk;
		break;
		case 12:
		PORTE.OUTCLR = D12_msk;
		break;
		
	}
}

void dioDirSet(uint8_t pinNumber)
{
	switch(pinNumber)
	{
		case 0:
		PORTC.DIRSET = D0_msk;
		daqAlloc.dioAlloc0.dio0Alloc = FEATURE_UTILIZED;
		break;
		case 1:
		PORTD.DIRSET = D1_msk;
		daqAlloc.dioAlloc0.dio1Alloc = FEATURE_UTILIZED;
		break;
		case 2:
		PORTD.DIRSET = D2_msk;
		daqAlloc.dioAlloc0.dio2Alloc = FEATURE_UTILIZED;
		break;
		case 3:
		PORTD.DIRSET = D3_msk;
		daqAlloc.dioAlloc0.dio3Alloc = FEATURE_UTILIZED;
		break;
		case 4:
		PORTD.DIRSET = D4_msk;
		daqAlloc.dioAlloc0.dio4Alloc = FEATURE_UTILIZED;
		break;
		case 5:
		PORTD.DIRSET = D5_msk;
		daqAlloc.dioAlloc0.dio5Alloc = FEATURE_UTILIZED;
		break;
		case 6:
		PORTD.DIRSET = D6_msk;
		daqAlloc.dioAlloc0.dio6Alloc = FEATURE_UTILIZED;
		break;
		case 7:
		PORTR.DIRSET = D7_msk;
		daqAlloc.dioAlloc0.dio7Alloc = FEATURE_UTILIZED;
		break;
		case 8:
		PORTR.DIRSET = D8_msk;
		daqAlloc.dioAlloc1.dio8Alloc = FEATURE_UTILIZED;
		break;
		case 9:
		PORTE.DIRSET = D9_msk;
		daqAlloc.dioAlloc1.dio9Alloc = FEATURE_UTILIZED;
		break;
		case 10:
		PORTE.DIRSET = D10_msk;
		daqAlloc.dioAlloc1.dio10Alloc = FEATURE_UTILIZED;
		break;
		case 11:
		PORTE.DIRSET = D11_msk;
		daqAlloc.dioAlloc1.dio11Alloc = FEATURE_UTILIZED;
		break;
		case 12:
		PORTE.DIRSET = D12_msk;
		daqAlloc.dioAlloc1.dio12Alloc = FEATURE_UTILIZED;
		break;
		
	}
}

void dioDirClr(uint8_t pinNumber)
{
	switch(pinNumber)
	{
		case 0:
		PORTC.DIRCLR = D0_msk;
		daqAlloc.dioAlloc0.dio0Alloc = FEATURE_FREE;
		break;
		case 1:
		PORTD.DIRCLR = D1_msk;
		daqAlloc.dioAlloc0.dio1Alloc = FEATURE_FREE;
		break;
		case 2:
		PORTD.DIRCLR = D2_msk;
		daqAlloc.dioAlloc0.dio2Alloc = FEATURE_FREE;
		break;
		case 3:
		PORTD.DIRCLR = D3_msk;
		daqAlloc.dioAlloc0.dio3Alloc = FEATURE_FREE;
		break;
		case 4:
		PORTD.DIRCLR = D4_msk;
		daqAlloc.dioAlloc0.dio4Alloc = FEATURE_FREE;
		break;
		case 5:
		PORTD.DIRCLR = D5_msk;
		daqAlloc.dioAlloc0.dio5Alloc = FEATURE_FREE;
		break;
		case 6:
		PORTD.DIRCLR = D6_msk;
		daqAlloc.dioAlloc0.dio6Alloc = FEATURE_FREE;
		break;
		case 7:
		PORTR.DIRCLR = D7_msk;
		daqAlloc.dioAlloc0.dio7Alloc = FEATURE_FREE;
		break;
		case 8:
		PORTR.DIRCLR = D8_msk;
		daqAlloc.dioAlloc1.dio8Alloc = FEATURE_FREE;
		break;
		case 9:
		PORTE.DIRCLR = D9_msk;
		daqAlloc.dioAlloc1.dio9Alloc = FEATURE_FREE;
		break;
		case 10:
		PORTE.DIRCLR = D10_msk;
		daqAlloc.dioAlloc1.dio10Alloc = FEATURE_FREE;
		break;
		case 11:
		PORTE.DIRCLR = D11_msk;
		daqAlloc.dioAlloc1.dio11Alloc = FEATURE_FREE;
		break;
		case 12:
		PORTE.DIRCLR = D12_msk;
		daqAlloc.dioAlloc1.dio12Alloc = FEATURE_FREE;
		break;
	}
}

void dioSense(PORT_ISC_t isc, uint8_t pinNumber)
{
	switch(pinNumber)
	{
		case 0:
		PORTC.PIN1CTRL &= ~PORT_ISC_gm;
		PORTC.PIN1CTRL |= isc;
		break;
		case 1:
		PORTD.PIN0CTRL &= ~PORT_ISC_gm;
		PORTD.PIN0CTRL |= isc;
		break;
		case 2:
		PORTD.PIN1CTRL &= ~PORT_ISC_gm;
		PORTD.PIN1CTRL |= isc;
		break;
		case 3:
		PORTD.PIN2CTRL &= ~PORT_ISC_gm;
		PORTD.PIN2CTRL |= isc;
		break;
		case 4:
		PORTD.PIN3CTRL &= ~PORT_ISC_gm;
		PORTD.PIN3CTRL |= isc;
		break;
		case 5:
		PORTD.PIN4CTRL &= ~PORT_ISC_gm;
		PORTD.PIN4CTRL |= isc;
		break;
		case 6:
		PORTD.PIN5CTRL &= ~PORT_ISC_gm;
		PORTD.PIN5CTRL |= isc;
		break;
		case 7:
		PORTR.PIN1CTRL &= ~PORT_ISC_gm;
		PORTR.PIN1CTRL |= isc;
		break;
		case 8:
		PORTR.PIN0CTRL &= ~PORT_ISC_gm;
		PORTR.PIN0CTRL |= isc;
		break;
		case 9:
		PORTE.PIN3CTRL &= ~PORT_ISC_gm;
		PORTE.PIN3CTRL |= isc;
		break;
		case 10:
		PORTE.PIN2CTRL &= ~PORT_ISC_gm;
		PORTE.PIN2CTRL |= isc;
		break;
		case 11:
		PORTE.PIN1CTRL &= ~PORT_ISC_gm;
		PORTE.PIN1CTRL |= isc;
		break;
		case 12:
		PORTE.PIN0CTRL &= ~PORT_ISC_gm;
		PORTE.PIN0CTRL |= isc;
		break;
	}
}

uint8_t dioReadLow()
{		
	uint8_t dataR,dataD,dataC,retVal = 0;
	
	dataR = PORTR.IN;
	dataD = PORTD.IN;
	dataC = PORTC.IN;
	
	if(dataR & D7_msk)retVal |= (1)<<7;
	if(dataD & D6_msk)retVal |= (1)<<6	;
	if(dataD & D5_msk)retVal |= (1)<<5	;
	if(dataD & D4_msk)retVal |= (1)<<4	;
	if(dataD & D3_msk)retVal |= (1)<<3	;
	if(dataD & D2_msk)retVal |= (1)<<2	;
	if(dataD & D1_msk)retVal |= (1)<<1	;
	if(dataC & D0_msk)retVal |= (1)<<0	;
	
	return retVal;
						
}
uint8_t dioReadHigh()
{
	
	uint8_t dataE,dataR,retVal = 0;
		
	dataE = PORTE.IN;
	dataR = PORTR.IN;
	
	if(dataE & PFIO_msk)	retVal |= (1)<<4;
	if(dataE & D11_msk)		retVal |= (1)<<3;
	if(dataE & D10_msk)		retVal |= (1)<<2;
	if(dataE & D9_msk)		retVal |= (1)<<1;
	if(dataR & D8_msk)		retVal |= (1)<<0;
	
	return retVal;
}